//
//  JHShowPracticeDataCell.m
//  JuniRengLish_iOS
//
//  Created by Maiyou on 2019/6/3.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHShowPracticeDataCell.h"

@implementation JHShowPracticeDataCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self.option1 layoutButtonWithImageStyle:ZJButtonImageStyleLeft imageTitleToSpace:10];
    [self.option2 layoutButtonWithImageStyle:ZJButtonImageStyleLeft imageTitleToSpace:10];
    [self.option3 layoutButtonWithImageStyle:ZJButtonImageStyleLeft imageTitleToSpace:10];
    [self.option4 layoutButtonWithImageStyle:ZJButtonImageStyleLeft imageTitleToSpace:10];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setDataModel:(JHPracticeDataModel *)dataModel
{
    _dataModel = dataModel;
    
    self.questionLabel.text = dataModel.question;
    
    [self.option1 setTitle:[NSString stringWithFormat:@"%@ x", dataModel.option1] forState:UIControlStateSelected];
    
    [self.option2 setTitle:[NSString stringWithFormat:@"%@ x", dataModel.option2] forState:UIControlStateSelected];
    
    [self.option3 setTitle:[NSString stringWithFormat:@"%@ x", dataModel.option3] forState:UIControlStateSelected];
    
    [self.option4 setTitle:[NSString stringWithFormat:@"%@ x", dataModel.option4] forState:UIControlStateSelected];
    
    switch (dataModel.answer.integerValue) {
        case 0:
            [self.option1 setTitle:[NSString stringWithFormat:@"%@ v", dataModel.option1] forState:UIControlStateSelected];
            [self.option1 setTitleColor:JKGetColor(@"009051") forState:UIControlStateSelected];
            break;
        case 1:
            [self.option2 setTitle:[NSString stringWithFormat:@"%@ v", dataModel.option2] forState:UIControlStateSelected];
            [self.option2 setTitleColor:JKGetColor(@"009051") forState:UIControlStateSelected];
            break;
        case 2:
            [self.option3 setTitle:[NSString stringWithFormat:@"%@ v", dataModel.option3] forState:UIControlStateSelected];
            [self.option3 setTitleColor:JKGetColor(@"009051") forState:UIControlStateSelected];
            break;
        case 3:
            [self.option4 setTitle:[NSString stringWithFormat:@"%@ v", dataModel.option4] forState:UIControlStateSelected];
            [self.option4 setTitleColor:JKGetColor(@"009051") forState:UIControlStateSelected];
            break;
            
        default:
            break;
    }
    
    [self.option1 setTitle:dataModel.option1 forState:0];
    
    [self.option2 setTitle:dataModel.option2 forState:0];
    
    [self.option3 setTitle:dataModel.option3 forState:0];
    
    [self.option4 setTitle:dataModel.option4 forState:0];
    
    if (dataModel.isChoose)
    {
        if (dataModel.choosedOption == dataModel.answer.integerValue)
        {
            self.answerLabel.text = @"回答正确";
            self.answerLabel.textColor = JKGetColor(@"009051");
            UIButton * button = [self.contentView viewWithTag:dataModel.answer.integerValue + 10];
            button.selected = YES;
            [button setImage:[UIImage imageNamed:@"已选择1"] forState:UIControlStateSelected];
        }
        else
        {
            self.answerLabel.text = [NSString stringWithFormat:@"回答错误，正确答案是：%@", [self changeAnswer]];
            self.answerLabel.textColor = [UIColor redColor];
            
            UIButton * button = [self.contentView viewWithTag:dataModel.answer.integerValue + 10];
            button.selected = YES;
            
            UIButton * button1 = [self.contentView viewWithTag:dataModel.choosedOption + 10];
            button1.selected = YES;
            
            [button setImage:[UIImage imageNamed:@"未选择"] forState:0];
            [button1 setImage:[UIImage imageNamed:@"已选择1"] forState:0];
        }
        self.explainLabel.text = dataModel.explain;
    }
    else
    {
        self.answerLabel.text = @"";
        
        self.explainLabel.text = @"";
    }
}

- (IBAction)optionBtnClick:(id)sender
{
    UIButton * button = sender;
    NSInteger index = button.tag - 10;
    
    self.dataModel.isChoose = YES;
    self.dataModel.choosedOption = index;
    if (self.selectedButton != button)
    {
        button.selected = YES;
        self.selectedButton.selected = NO;
        self.selectedButton = button;
        
        self.selectedOption();
    }
}

- (NSString *)changeAnswer
{
    NSString * str = @"A";
    switch (_dataModel.answer.integerValue) {
        case 0:
            str = @"A";
            break;
        case 1:
            str = @"B";
            break;
        case 2:
            str = @"C";
            break;
        case 3:
            str = @"D";
            break;
            
        default:
            break;
    }
    return str;
}

@end
